$(function() {
	//点击上级菜单input事件
	$('#parentName').click(function() {
		utils.plyopen({
			title: '选择上级菜单',
			area: ['300px', '400px'],
			content: 'menutree',
			btn:['确定', '取消'],
			yes: function(index, layero) {
				var treeNode = utils.getLayerWin(layero).treeNode;
				if(treeNode == null) {
					parent.layer.alert('请选择上级菜单');
					return false;
				}
				$('#parentId').val(treeNode.id);
				$('#parentName').val(treeNode.name);
				parent.layer.close(index);
			}
		});
	});
	
	$('#form').validate({
		rules:{
			menuName: {
				required:true,
				maxlength: 45
			},
			menuType: {
				required:true				
			},
			icon: {
				maxlength: 45
			},
			url: {
				maxlength: 255				
			},
			sort: {
				number:true
			}
		},
		messages:{
			menuName:{
				required:'请输入名称',
				maxlength: '只允许输入45个以内字符'
			},
			menuType: {
				required:'请选择菜单类型'
			},
			icon: {
				maxlength: '只允许输入45个以内字符'				
			},
			url: {
				maxlength: '只允许输入200个以内字符'				
			},
			sort: {
				number: '请输入数字'
			}
		},
        submitHandler:function(form){
        	var json = $('#form').serializeJson();
        	if(validation.isEmpty(json.menuId)) {
        		save(json); 		
        	}else {
        		update(json);
        	}
        	return false;
        }
	});
	
	//保存
	function save(json) {
		$.loadReq({
			type:'post',
			url:'save',
			data:json,
			success: function(res) {
				if(res.status == 'true') {
					utils.palert('添加成功', function() {
						utils.closeLayer();
					});
				} else {
					utils.palert(res.msg);
				}
			}
		});
	}
	
	//修改
	function update(json) {
		$.loadReq({
			type:'post',
			url:'edit',
			data:json,
			success: function(res) {
				if(res.status == 'true') {
					utils.palert('修改成功', function() {
						utils.closeLayer();
					});
				} else {
					utils.palert(res.msg);
				}
			}
		});
	}
	
	$('.b-btn-save').click(function() {
		$('form').submit();
	});
});